import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author SaoE
 * @date 2025/3/11 17:11
 */
public class _56 {
        List<int[]> res = new ArrayList<>();
    public int[][] merge(int[][] intervals) {
        if (intervals.length == 1){
            return intervals;
        }
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        res.add(intervals[0]);
        for(int i = 1; i < intervals.length; i++){
            put(intervals[i]);
        }
        return res.toArray(new int[res.size()][]);
    }
    public void put(int[] intervals){
        int[] last = res.get(res.size() - 1);
        if (intervals[1] <= last[1]){
            return;
        }else if (intervals[0] > last[1]){
            res.add(intervals);
        }else {
            last[1] = Math.max(last[1], intervals[1]);
            res.set(res.size() - 1, last);
        }
    }

    public static void main(String[] args) {
        _56 a = new _56();
        // [[2,3],[2,2],[3,3],[1,3],[5,7],[2,2],[4,6]]
        int[][] intervals = {{2,3}, {2,2}, {3,3}, {1,3}, {5,7}, {2,2}, {4,6}};

//        int[][] intervals = {{1,3},{2,6},{8,10},{15,18}};
        a.merge(intervals);
        System.out.println(Arrays.deepToString(a.merge(intervals)));
    }
}
